﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PortChat
{
    //add an error to an input string
    class ErrorGenerator
    {
        //error word
        //private Boolean[] bArrError = { false, false, false, false, false, false, false, true };
        private Boolean[] bArrError = new Boolean[8];
        private char[] charArrError = new char[8];

        //sets an error word
        public void setError(Boolean[] bArrErrorP)
        {
            bArrError = bArrErrorP;
        }

        //sets an error word
        public void setError(String strErrorP)
        {
            charArrError = strErrorP.ToCharArray();
            int position = 0;

            foreach (char myCharArrError in charArrError)
            {
                if (myCharArrError.Equals('0'))
                {
                    bArrError[position] = false;
                }
                else
                {
                    bArrError[position] = true;
                }
                position++;
            }            
        }

        //apply an OR Operation to strInput
        public String addError(String strInputP)
        {
            //binary message as CharArray
            //p.e.:   0,0,0,0,0,0,0,1
            Char[] charArrayInput = strInputP.ToCharArray();
            int position = 0;
            //input as BooleanArray p.e. false, false, false, false, false, false, false, true
            Boolean[] bArrInput = new Boolean[strInputP.Length];
            foreach (char myCharArrInput in charArrayInput)
            {
                if (myCharArrInput.Equals('0'))
                {
                    bArrInput[position] = false;
                } else 
                {
                    bArrInput[position] = true;
                }
                position++;
            }
            position = 0;
            //output as boolean array; OR combination with error word and input
            Boolean[] bArrOutput = new Boolean[strInputP.Length];
            foreach (Boolean mybArrError in bArrError)
            {
                if (mybArrError.Equals(true))
                {
                    bArrOutput[position] = !bArrInput[position];
                }
                else
                {
                    bArrOutput[position] = bArrInput[position];
                }
                position++;
            }
            String strArrOutput = "";
            //convert boolean array into a string
            foreach (Boolean myBool in bArrOutput)
            {
                if (myBool)
                {
                    strArrOutput = strArrOutput + "1";
                }
                else
                {
                    strArrOutput = strArrOutput + "0";
                }
            }            
            //Console.WriteLine("strArrOutput: " + strArrOutput);
            return strArrOutput;
        }
    }
}
